package com.eelpo.cmmp.uac.pages.user;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.eelpo.cmmp.core.exception.ServiceException;
import com.eelpo.cmmp.core.wicket.behavior.JavascriptEventConfirmation;
import com.eelpo.cmmp.core.wicket.page.extensions.SerachListWebPage;
import com.eelpo.cmmp.core.wicket.panel.grid.column.ColumnLocation;
import com.eelpo.cmmp.core.wicket.panel.grid.column.ColumnLocation.Unit;
import com.eelpo.cmmp.core.wicket.panel.grid.column.IColumnHeader;
import com.eelpo.cmmp.core.wicket.panel.grid.column.OperateColumn;
import com.eelpo.cmmp.core.wicket.panel.grid.column.PropertyColumn;
import com.eelpo.cmmp.uac.dao.vo.User;
import com.eelpo.cmmp.uac.dao.vo.enumed.AuthMode;
import com.eelpo.cmmp.uac.services.IUserManageService;
import com.eelpo.cmmp.uac.session.UACSession;

import org.apache.wicket.MarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.Loop.LoopItem;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

/**
 * 
 * <pre>
 * 
 * 用户管理页面
 * 
 * </pre>
 * 
 * @author <a href="mailto:Brian@eelpo.com">汪洪州</a>
 * @since 1.0
 */
public class UserManage extends SerachListWebPage<User> {

	@SpringBean
	private IUserManageService userManageService;

	public UserManage() {
		super("用户管理", 15, new User());
	}

	@Override
	public Panel renderSearchPanel(String id) {
		return new UserSearchPanel(id, getParameter());
	}

	@Override
	public Iterator<? extends User> iterator(User parameter, int first, int count) throws Exception {
		return userManageService.getUsersPaing(UACSession.get().getUserId(), parameter, first, count).iterator();
	}

	@Override
	public int totalRows(User parameter) throws Exception {
		return userManageService.getUsersCount(UACSession.get().getUserId(), parameter);
	}

	@Override
	public List<IColumnHeader<User>> renderColumns() {
		List<IColumnHeader<User>> columns = new ArrayList<IColumnHeader<User>>();
		columns.add(new PropertyColumn<User>("用户名称", "name", true, new ColumnLocation(18, Unit.PERCENT)));
		columns.add(new PropertyColumn<User>("用户帐号", "account", true, new ColumnLocation(9, Unit.PERCENT)));
		columns.add(new PropertyColumn<User>("用户手机", "mobile", true, new ColumnLocation(9, Unit.PERCENT)));
		columns.add(new PropertyColumn<User>("页面登录", "allowWebLogin.label", "allowWebLogin", true, new ColumnLocation(9, Unit.PERCENT)));
		columns.add(new PropertyColumn<User>("页面鉴权", "webAuthMode.label", "webAuthMode", true, new ColumnLocation(9, Unit.PERCENT)));
		columns.add(new PropertyColumn<User>("接口登录", "allowInterface.label", "allowInterface", true, new ColumnLocation(9, Unit.PERCENT)));
		columns.add(new PropertyColumn<User>("接口鉴权", "interfaceAuthMode.label", "interfaceAuthMode", true, new ColumnLocation(9, Unit.PERCENT)));
		columns.add(new PropertyColumn<User>("用户状态", "status.label", "status", true, new ColumnLocation(9, Unit.PERCENT)));
		columns.add(new OperateColumn<User>(3, new ColumnLocation(19, Unit.PERCENT)) {

			private static final long serialVersionUID = -2091936045710639843L;

			@Override
			public void populateItem(String id, String labelId, LoopItem item, IModel<User> model) {
				switch (item.getIteration()) {
				case 0:
					item.add(renderIpFilterLink(id, labelId, model));
					break;
				case 1:
					item.add(renderEditLink(id, labelId, model));
					break;
				case 2:
					item.add(renderDeleteLink(id, labelId, model));
					break;
				}
			}
		});
		return columns;
	}

	public MarkupContainer renderIpFilterLink(String id, String labelId, final IModel<User> model) {
		Link<User> ipFilter = new Link<User>(id) {

			private static final long serialVersionUID = -5260736556615893110L;

			@Override
			public void onClick() {
				setResponsePage(new AddOrEditUser(model.getObject()));
			}
		};

		// Web鉴权或者接口鉴权方式有使用IP的情况才显示
		ipFilter.setVisible(!model.getObject().getWebAuthMode().equals(AuthMode.PASSWORD) || !model.getObject().getInterfaceAuthMode().equals(AuthMode.PASSWORD));

		return ipFilter.add(new Label(labelId, "IP过滤"));
	}

	public MarkupContainer renderEditLink(String id, String labelId, final IModel<User> model) {
		Link<User> editLink = new Link<User>(id) {

			private static final long serialVersionUID = -5260736556615893110L;

			@Override
			public void onClick() {
				setResponsePage(new AddOrEditUser(model.getObject()));
			}
		};

		return editLink.add(new Label(labelId, "编辑"));
	}

	public MarkupContainer renderDeleteLink(String id, String labelId, final IModel<User> model) {
		Link<User> deleteLink = new Link<User>(id) {

			private static final long serialVersionUID = -5260736556615893110L;

			@Override
			public void onClick() {
				try {
					userManageService.deleteUser(model.getObject());
				} catch (ServiceException ex) {
					info(ex.getMessage());
				} catch (Exception e) {
					e.printStackTrace();
					info("删除用户失败！");
				}
			}
		};

		//deleteLink.setEnabled(CMMPSession.get().checkPermission("", ""));

		deleteLink.add(new JavascriptEventConfirmation("您确定要删除该用户吗？", deleteLink.isEnabled()));

		return deleteLink.add(new Label(labelId, "删除"));
	}
}
